package com.itboye.textileexpres.collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.tbshopcar.listener.UIAlertView;
import com.itboye.textileexpes.beans.CollectionBeans;
import com.itboye.textileexpes.beans.ColletionFenyeBeans;
import com.itboye.textileexpes.presenter.CategoryPresenter;
import com.itboye.textileexpes.presenter.ColloctionPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.collection.CollectionGoodsListViewAdapter.ViewHolder;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexpres.widget.SwipeMenu;
import com.itboye.textileexpres.widget.SwipeMenuCreator;
import com.itboye.textileexpres.widget.SwipeMenuItem;
import com.itboye.textileexpres.widget.SwipeMenuListView;
import com.itboye.textileexpres.widget.XListView;
import com.itboye.textileexpres.widget.XListView.IXListViewListener;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActivityCollectionEdit extends BaseActivity implements Observer,
		OnClickListener, LCallBack, IXListViewListener {
	ImageView back;
	TextView title;
	TextView edit;
	TextView goods_num;
	XListView goods_list;
	LinearLayout bottom_lin;
	RelativeLayout cancle_collection_rel, to_top_rel, order_rel, cancle_rel;
	boolean ed_flag = false;
	MyListViewAdapter listViewAdapter;
	EditText key_word;
	ColletionFenyeBeans beans = new ColletionFenyeBeans();
	ColloctionPresenter presenter;
	String uid;
	String type = "clean";
	int pagesize = 1;
	List<String> ids = new ArrayList<String>();

	List<ColletionFenyeBeans.InfoBeans> list = new ArrayList<ColletionFenyeBeans.InfoBeans>();
	RelativeLayout collection_rel, null_collection;
	boolean ischek;
	private ImageView img_other;
	UIAlertView dialog;

	String cateID;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.actvity_collection_edit;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		beans = new ColletionFenyeBeans();
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		presenter = new ColloctionPresenter(this);
		
		presenter.FenYeQuery(uid, "15", pagesize + "", "", "", "1");
		title.setText(R.string.collection);
		goods_list.setPullLoadEnable(true);
		goods_list.setPullRefreshEnable(true);
		goods_list.setXListViewListener(this);
		listViewAdapter = new MyListViewAdapter(this, list);
		goods_list.setAdapter(listViewAdapter);
		img_other.setVisibility(View.GONE);
		img_other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityCollectionEdit.this,
						ActivityCollectionEdit.class);
				startActivity(intent);
			}
		});

	}

	
	/***
	 * quxioa
	 * @param view
	 */
	
	public void btnClearateList(View view) {
		ids.clear();
		if (listViewAdapter.flage) {
			for (int i = 0; i < beans.getList().size(); i++) {
				if (beans.getList().get(i).isCheck) {
					ids.add(beans.getList().get(i).getId());
					beans.setIds(ids.toString().substring(1,
							ids.toString().length() - 1));
					System.out.println("modelmodelmodelisd"
							+ beans.getList().get(i).getId());

				}
			}
			if (ids.toString().length() > 2) {
				// toast(ids.toString().substring(1, ids.toString().length() -
				// 1));
				beans.setUid(uid);
				beans.setIds(ids.toString().substring(1,
						ids.toString().length() - 1));

			} else {
				toast(getString(R.string.h1188));
			}
		}
	}
	public void btnOperateList(View view) {
		ids.clear();
		if (listViewAdapter.flage) {
			for (int i = 0; i < beans.getList().size(); i++) {
				if (beans.getList().get(i).isCheck) {
					ids.add(beans.getList().get(i).getFavorite_id());
					beans.setIds(ids.toString().substring(1,
							ids.toString().length() - 1));
					System.out.println("modelmodelmodelisd"
							+ beans.getList().get(i).getFavorite_id());

				}
			}
			if (ids.toString().length() > 2) {
				// toast(ids.toString().substring(1, ids.toString().length() -
				// 1));
				beans.setUid(uid);
				beans.setIds(ids.toString().substring(1,
						ids.toString().length() - 1));

			} else {
				toast(getString(R.string.h1188));
			}
		}
	}

	/**
	 * 确认取消收藏
	 * 
	 * @param model
	 */
	private void showNoticeDialog(final ColletionFenyeBeans model,final String idsto) {

		dialog = new UIAlertView(this, "", getString(R.string.h1198),
				getString(R.string.cancel), getString(R.string.h1129));
		dialog.show();

		dialog.setClicklistener(new UIAlertView.ClickListenerInterface() {

			public void doLeft() {

				dialog.dismiss();
			}

			public void doRight() {
				model.setIds(idsto);
				presenter.delete(model);
				dialog.dismiss();
			}
		});
	}

	public void isBtnSend(View view) {
		ids.clear();
		if (listViewAdapter.flage) {
			for (int i = 0; i < beans.getList().size(); i++) {
				if (beans.getList().get(i).isCheck) {
					ids.add(beans.getList().get(i).getFavorite_id());

				}
			}
			if (ids.toString().length() > 2) {
				OnButtonOperateList(view);
				// toast(ids.toString().substring(1, ids.toString().length() -
				// 1));
				beans.setUid(uid);
				beans.setIds(ids.toString().substring(1,
						ids.toString().length() - 1));
			} else {
				toast(getString(R.string.h104));
			}
		}
	}

	public void OnButtonOperateList(View view) {

		Intent intent2 = new Intent(ActivityCollectionEdit.this,
				ActivityIsOrder.class);
		intent2.putExtra("book", beans);
		intent2.putExtra("ids",
				ids.toString().substring(1, ids.toString().length() - 1));
		startActivity(intent2);
		System.out.println("ischekischek" + ischek);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		type = "clean";
		pagesize = 1;
		presenter.FenYeQuery(uid, "10", pagesize + "", "", "", "1");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		type = "load";
		pagesize++;
		presenter.FenYeQuery(uid, "10", pagesize + "", "", "", "1");
	}

	void stop() {
		goods_list.stopLoadMore();
		goods_list.stopRefresh();
	}

	@Override
	public void answer(int str) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.edit:
			if (ed_flag) {
				bottom_lin.setVisibility(View.VISIBLE);
			} else {
				bottom_lin.setVisibility(View.GONE);
			}
			ed_flag = !ed_flag;
			Intent up = new Intent("UpDateCollection");
			sendBroadcast(up);
			finish();
			break;
		case R.id.key_word:
			Intent intent = new Intent(ActivityCollectionEdit.this,
					ActivityCollectionSeaech.class);
			startActivity(intent);
			break;
		case R.id.cancle_rel:
			btnClearateList(v);
			if (ids.toString().length() > 2) {
				System.out.println("uiduiduiudid" + ids.toString());
				System.out.println("modelmodelmodel" + ids.toString());
				showNoticeDialog(beans,ids.toString());

			} else {
				// toast("请选择一个");
			}
			break;
		case R.id.to_top_rel:
			btnOperateList(v);
			if (ids.toString().length() > 2) {
				presenter.update(beans);
			}
			break;
		case R.id.order_rel:
			isBtnSend(v);
			break;
		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ColloctionPresenter.ColloctFenye_success) {

				if (type.equals("load")) {

				} else {
					list.clear();
				}
				beans = (ColletionFenyeBeans) entity.getData();
				list.addAll(beans.getList());
				if (list.size() == 0) {
					collection_rel.setVisibility(View.GONE);
					null_collection.setVisibility(View.VISIBLE);
				} else {
					collection_rel.setVisibility(View.VISIBLE);
					null_collection.setVisibility(View.GONE);
				}
				// Log.e("hu", beans.getCount() + "");
				goods_num.setText(beans.getCount() + "");
				listViewAdapter.notifyDataSetChanged();
				stop();
			} else if (entity.getEventType() == ColloctionPresenter.ColloctFenye_fail) {
				toast(entity.getData() + "");
			}
			if (entity.getEventType() == ColloctionPresenter.ColloctDelete_success) {
				presenter.FenYeQuery(uid, "10", "1", "", "", "1");
				listViewAdapter.notifyDataSetChanged();
			} else if (entity.getEventType() == ColloctionPresenter.ColloctDelete_fail) {
				toast("" + entity.getData());
			}
			if (entity.getEventType() == ColloctionPresenter.ColloctTop_success) {

				presenter.FenYeQuery(uid, "10", "1", "", "", "1");
				listViewAdapter.notifyDataSetChanged();
			} else if (entity.getEventType() == ColloctionPresenter.ColloctTop_fail) {
				toast("" + entity.getData());
			}
			if (entity.getEventType() == ColloctionPresenter.ColloctOrder_success) {
				toast("" + entity.getData());
			} else if (entity.getEventType() == ColloctionPresenter.ColloctOrder_fail) {
				toast("" + entity.getData());
			}
		}
	}

	class MyListViewAdapter extends BaseAdapter {
		public boolean flage = true;
		boolean flag;
		public Map<Integer, String> selected;
		Context context;
		List<ColletionFenyeBeans.InfoBeans> list;
	

		public MyListViewAdapter(Context context,
				List<ColletionFenyeBeans.InfoBeans> list) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.list = list;
			selected = new HashMap<Integer, String>();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = (LinearLayout) inflater.inflate(
						R.layout.item_collection_edit, null);
				holder.item_goods_lin = (LinearLayout) convertView
						.findViewById(R.id.item_goods_lin);
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				holder.checked = (CheckBox) convertView
						.findViewById(R.id.checked);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.delete_img.setVisibility(View.GONE);
			holder.checked.setTag(position);
			if (selected.containsKey(position)) {

				holder.checked.setChecked(true);
			} else {
				holder.checked.setChecked(false);
			}
			// 根据isSelected来设置checkbox的显示状况
			// if (flage) {
			// holder.checked.set;
			// } else {
			// holder.checked.setChecked(false);
			// }

			// 注意这里设置的不是onCheckedChangListener，还是值得思考一下的
			holder.checked.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (list.get(position).isCheck) {
						list.get(position).isCheck = false;
					} else {
						list.get(position).isCheck = true;
					}
				}
			});
			holder.context.setText(list.get(position).getFavorite_content()
					.getSecondary());
			holder.title.setText(list.get(position).getFavorite_content()
					.getName());
			// holder.publish_time.setText("时间:"
			// + TimeFormat.DateFormat(list.get(position)
			// .getFavorite_content().getCreate_time()));
			XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
					+ list.get(position).getFavorite_content().getId(),
					holder.goods_img);
			holder.number.setText(R.string.h2
					+ ":"
					+ list.get(position).getFavorite_content()
							.getProduct_code());

			return convertView;
		}

		class ViewHolder {
			TextView publish_time, title, context, number;
			LinearLayout item_goods_lin;
			ImageView goods_img, collection_img, delete_img;
			CheckBox checked;
		}

	}

}

interface LCallBack {
	public void answer(int str); // 类B的内部接口

}